package com.huike.xingxing.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.huike.xingxing.R;
import com.huike.xingxing.api.Api;
import com.huike.xingxing.ui.CircleImageView;

import org.json.JSONException;
import org.json.JSONObject;

public class DailyStatusActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_title_black;
    private TextView tv_title_txt_black;
    private TextView tv_title_txt_content;
    private ImageView iv_title_right_image;
    private TextView tv_title_right_txt;
    private LinearLayout ll_status_yiban;
    private LinearLayout ll_status_kaix;
    private LinearLayout ll_status_bukaix;
    private RequestQueue queue;
    private CircleImageView myimage;
    private String head_image;
    private String username;
    private String uuid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_status);
        queue = Volley.newRequestQueue(this);
        initView();
    }

    private void initView() {
        iv_title_black = (ImageView) findViewById(R.id.iv_title_black);
        tv_title_txt_black = (TextView) findViewById(R.id.tv_title_txt_black);
        tv_title_txt_content = (TextView) findViewById(R.id.tv_title_txt_content);
        iv_title_right_image = (ImageView) findViewById(R.id.iv_title_right_image);
        tv_title_right_txt = (TextView) findViewById(R.id.tv_title_right_txt);

        tv_title_txt_content.setText("每日状态");
        iv_title_black.setVisibility(View.VISIBLE);

        iv_title_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ll_status_yiban = (LinearLayout) findViewById(R.id.ll_status_yiban);
        ll_status_kaix = (LinearLayout) findViewById(R.id.ll_status_kaix);
        ll_status_bukaix = (LinearLayout) findViewById(R.id.ll_status_bukaix);

        ll_status_yiban.setOnClickListener(this);
        ll_status_kaix.setOnClickListener(this);
        ll_status_bukaix.setOnClickListener(this);
        myimage = (CircleImageView) findViewById(R.id.myimage);
        SharedPreferences userIfno = getSharedPreferences("user_ifno", MODE_PRIVATE);
         username = userIfno.getString("username", null);
         head_image = userIfno.getString("head_image", null);
         uuid = userIfno.getString("uuid", null);
        if(head_image!=null){
            Glide.with(this).load(Api.url+head_image).into(myimage);
        }



    }


    private String status = "happy";//选择状态

    @Override
    public void onClick(View v) {
        if (v == ll_status_yiban) {//选择一般
            status = "general";
            //设置选着状态
            ll_status_yiban.setBackgroundResource(R.drawable.state_select);
            ll_status_kaix.setBackgroundResource(R.drawable.state_no_select);
            ll_status_bukaix.setBackgroundResource(R.drawable.state_no_select);

        } else if (v == ll_status_kaix) {//选择开心
            status = "happy";

            ll_status_yiban.setBackgroundResource(R.drawable.state_no_select);
            ll_status_kaix.setBackgroundResource(R.drawable.state_select);
            ll_status_bukaix.setBackgroundResource(R.drawable.state_no_select);
        } else if (v == ll_status_bukaix) {//选择不开心
            status = "unhappy";
            ll_status_yiban.setBackgroundResource(R.drawable.state_no_select);
            ll_status_kaix.setBackgroundResource(R.drawable.state_no_select);
            ll_status_bukaix.setBackgroundResource(R.drawable.state_select);

        }
    }

    public void SaveStatus(String uid) {

        showLoading();
        JSONObject object = new JSONObject();
        try {
            object.put("fettle", status);
            object.put("uid", uid);
            object.put("head_img",head_image);
            object.put("uname",username);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("---", "SaveStatus: "+object.toString());
        JsonObjectRequest request = new JsonObjectRequest(1, Api.locaApi + "addMstudent", object.toString(), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();

                Log.e("---", jsonObject.toString());
                try {
                    String data = jsonObject.getString("data");
                    if (data.equals("ok") && !TextUtils.isEmpty(data)) {
                        Toast.makeText(DailyStatusActivity.this, "成功！", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(DailyStatusActivity.this, "失败:" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(DailyStatusActivity.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(request);
    }

    //提交
    public void mySubmit(View view) {

        SharedPreferences userIfno = getSharedPreferences("user_ifno", MODE_PRIVATE);
        String uid = userIfno.getString("uuid", null);
        Log.e("---", uid + "---");
        if (!TextUtils.isEmpty(uid)) {
            SaveStatus(uid);
        }

    }

}
